#ifndef __MYSQL_CONNECT_POLL__
#define __MYSQL_CONNECT_POLL__

#include <mysql/mysql.h>
#include <mutex>
#include <thread>
#include <queue>
#include "../Log/Log.h"
#include <semaphore.h>

class SqlConnPool
{
public:
    void init(const char *host, int port,
              const char *user, const char *pwd,
              const char *dbName, int connSize);

    static SqlConnPool *Instance();

    MYSQL *GetConn();

    void FreeConn(MYSQL *conn);

    int GetFreeConnCount();

    void Close();

private:
    SqlConnPool();
    ~SqlConnPool();

private:
    std::queue<MYSQL *> connQue_;
    sem_t sem_;
    std::mutex Mtx_;

    int freeCount_;
    int MaxCount_;
    int useCount_;
};

#endif